#include "Scuba.h"
#include "Funcalyzer.h"
#include "Submarine.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

CArgumentParser* gpxArgumentParser;
string gsTarget = "./test.c";

Scuba xScubaDiver;
Funcalyzer xFuncalyzer;
Submarine gxSubmarine(&xFuncalyzer);

int parseCommandLineSubmarine(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("xcvdblrqnth");

	gpxArgumentParser->m_Helper->fillProgramName("nSubmarine");
	gpxArgumentParser->m_Helper->fillAuthor("SonOfDelphi");
	gpxArgumentParser->m_Helper->fillSummary("Function Information Analyzer");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <SourceFile>");
	gpxArgumentParser->fillHelpForOption('x',"Complexity information          :default");
	gpxArgumentParser->fillHelpForOption('c',"Calls made by function          :one-level support");
	gpxArgumentParser->fillHelpForOption('v',"Values returned by function     :simple only, buggy?");
	gpxArgumentParser->fillHelpForOption('d',"Data/Variables used by function :");
	gpxArgumentParser->fillHelpForOption('b',"Bare (No title row)");
	gpxArgumentParser->fillHelpForOption('l',"List methods only");
	gpxArgumentParser->fillHelpForOption('r',"Report (Text format)");
	gpxArgumentParser->fillHelpForOption('q',"Quiet mode");
	gpxArgumentParser->fillHelpForOption('n',"No Numbering");
	gpxArgumentParser->fillHelpForOption('t',"HTML output");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}
	
	gxSubmarine.m_bComplexity = (gpxArgumentParser->wasOptionSupplied('x'));
	gxSubmarine.m_bCalls = (gpxArgumentParser->wasOptionSupplied('c'));
	gxSubmarine.m_bReturns = (gpxArgumentParser->wasOptionSupplied('v'));
	gxSubmarine.m_bDataUse = (gpxArgumentParser->wasOptionSupplied('d'));

	gxSubmarine.m_bNoHeading = gpxArgumentParser->wasOptionSupplied('b');
	gxSubmarine.m_bListOnly = gpxArgumentParser->wasOptionSupplied('l');
	gxSubmarine.m_bReport = gpxArgumentParser->wasOptionSupplied('r');
	gxSubmarine.m_bModeQuiet = gpxArgumentParser->wasOptionSupplied('q');
	gxSubmarine.m_bNoNumbering = gpxArgumentParser->wasOptionSupplied('n');
	gxSubmarine.m_bHTML = gpxArgumentParser->wasOptionSupplied('t');

	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineSubmarine(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
		if(!gxSubmarine.m_bModeQuiet)
			cerr<<"Attempting to proceed with default test file: "<<gsTarget<<endl;
	}

    if(!gxSubmarine.m_bModeQuiet)
		cerr<<"\n<<"<<gsTarget<<">>"<< " FUNCTION INFORMATION\n\n";

	xScubaDiver.addObserver(&xFuncalyzer);
	xScubaDiver.dive(gsTarget);

	gxSubmarine.print();

    if(!gxSubmarine.m_bModeQuiet)
	    cerr<<"\n<<"<<gsTarget<<">>"<< " END OF FUNCTION INFORMATION\n";
	if(argc==1)
	{
		cerr<<"Press any key to continue..";
		_getch();
	}

    return 0;
}


